﻿using System;
using System.Text;
using System.IO;

namespace ooWii.IO
{
	public class WiiDiscHeader
	{
		#region WiiDiscHeader

		char discId;
		string gameCode;
		char regionCode;
		string makerCode;
		byte discNumber;
		byte discVersion;
		byte audioStreaming;
		byte streamingBufferSize;
		UInt32 magicWord;
		string gameTitle;
		byte verifyHash;
		byte decrypt;

		public WiiDiscHeader( Stream iso )
		{
			byte[] buffer = new byte[ 0x400 ];

			iso.Position = 0;
			if( iso.Read( buffer, 0, buffer.Length ) != buffer.Length )
			{
				throw new WiiException( "Cannot read Wii disc header" );
			}

			discId = (char)buffer[ 0 ];
			gameCode = ASCIIEncoding.ASCII.GetString( buffer, 1, 2 );
			regionCode = (char)buffer[ 3 ];
			makerCode = ASCIIEncoding.ASCII.GetString( buffer, 4, 2 );
			discNumber = buffer[ 6 ];
			discVersion = buffer[ 7 ];
			audioStreaming = buffer[ 8 ];
			streamingBufferSize = buffer[ 9 ];
			magicWord = WiiBuffer.GetUInt32( buffer, 0x18 );
			gameTitle = ASCIIEncoding.ASCII.GetString( buffer, 0x20, 64 );
			verifyHash = buffer[ 0x60 ];
			decrypt = buffer[ 0x61 ];

			if( magicWord != 0x5D1C9EA3 )
			{
				throw new WiiException( "Invalid Wii disc header" );
			}
		}

		public char DiscId
		{
			get
			{
				return discId;
			}
		}

		public string GameCode
		{
			get
			{
				return gameCode;
			}
		}

		public char RegionCode
		{
			get
			{
				return regionCode;
			}
		}

		public string MakerCode
		{
			get
			{
				return makerCode;
			}
		}

		public byte DiscNumber
		{
			get
			{
				return discNumber;
			}
		}

		public byte DiscVersion
		{
			get
			{
				return discVersion;
			}
		}

		public byte AudioStreaming
		{
			get
			{
				return audioStreaming;
			}
		}

		public byte StreamingBufferSize
		{
			get
			{
				return streamingBufferSize;
			}
		}


		public UInt32 MagicWord
		{
			get
			{
				return magicWord;
			}
		}

		public string GameTitle
		{
			get
			{
				return gameTitle;
			}
		}

		public byte VerifyHash
		{
			get
			{
				return verifyHash;
			}
		}

		public byte Decrypt
		{
			get
			{
				return decrypt;
			}
		}

		#endregion
	}
}
